package com.xiaoxi.xirpc.server.tcp;

import com.xiaoxi.xirpc.server.HttpServer;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;

public class VertxTcpServer implements HttpServer {

    private byte[] handleRequest(byte[] requestData){
        return "hello client form vertx tcp server".getBytes();
    }

    @Override
    public void doStart(int port) {
        // 创建 vertx 实例
        Vertx vertx = Vertx.vertx();

        // 创建tcp服务器
        NetServer server = vertx.createNetServer();

        // 处理请求
        server.connectHandler(new TcpServerHandler());

        // 启动 tpc服务器并监听其端口
        server.listen(port, result -> {
            if (result.succeeded()){
                System.out.println("tpc server is now listening on port" + port);
            }else {
                System.out.println("failed to start tpc server:" + result.cause());
            }
        });
    }

    public static void main(String[] args) {
        new VertxTcpServer().doStart(9090);
    }
}
